﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Models
{
    public class IndTagsViewModel: BaseViewModel
    {
        private int _numPerPage = 15;

        public IndTagsViewModel()
            : base()
        { }

        public IndTagsViewModel(string useName, int loginID, string tag, int page)
            : base(new { UserName = useName, LoginID =  loginID, Tag = tag, Page = page})
        {

        }

        public Boolean IsIndAccountOwner { get; set; }
        public string AvatarUrl { get; set; }
        public Login Login { get; set; }
        public Ind Ind { get; set; }
        public List<Shot> ShotList { get; set; }
        public List<Skill> SkillList { get; set; }
        public List<STag> ShotTagList { get; set; }
        public string Tag { get; set; }
        public Boolean FilterByTag { get; set; }
        public int PreviousPage { get; set; }
        public int NextPage { get; set; }

        public Boolean IsIndSaved { get; set; }
        public Boolean IsSentRecently { get; set; }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            //init data
            string userName = dynParams.UserName;
            int loginID = dynParams.LoginID;
            string tag = dynParams.Tag;
            int page = dynParams.Page;
            Tag = tag;
            FilterByTag = !string.IsNullOrEmpty(tag);

            IsIndAccountOwner = Identity.IsIndAccountOwner(userName);
            
            IndLogin indLogin = DataUtils.GetIndLoginByID(loginID);
            Login = indLogin.Login;
            Ind = indLogin.Ind;
            SkillList = DataUtils.GetList<Skill>(ConstUtils.SP_GetSkillListByID, new { IndID = Ind.IndID });
            AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.IndAvatar, Ind.IndID, AvatarSizeEnum.Avatar_80x80, Ind.AvatarFileExtension);
            
            if (FilterByTag)
            {
                //if has tag filter, 
                ShotTagList = DataUtils.GetList<STag>(ConstUtils.SP_GetMostUsedShotTagStatCountByID, new { IndID = Ind.IndID, Top = 30 });
                //shot url
                ShotList = DataUtils.GetList<Shot>(ConstUtils.SP_GetShotsByIDAndTag, new { IndID = Ind.IndID, Tag = tag, Page = page, NumPerPage = _numPerPage });
                foreach (var shot in ShotList)
                {
                    shot["ShotUrl"] = ImageUtils.GetShotRelativeUrl(Ind.IndID, shot.ShotID, ShotSizeEnum.Shot_200x133, shot.FileExtension);
                }

                //get next page
                int totalCount = ShotList != null && ShotList.Count > 0 ? ShotList[0].TotalCount : 0;
                PreviousPage = CommonUtilities.GetPreviousPage(page);
                NextPage = CommonUtilities.GetNextPage(totalCount, page, _numPerPage);
            }
            else
            {
                //no filter, only show all tags
                ShotTagList = DataUtils.GetList<STag>(ConstUtils.SP_GetShotTagStatCountByID, new { IndID = Ind.IndID });
            }

            if (Identity.IsLoggedIn)
            {
                SavedInd savedInd = DataUtils.GetOne<SavedInd>(ConstUtils.SP_GetOneSavedInd, new { SaveLoginID = Identity.LoginID, SavedLoginID = loginID });
                IsIndSaved = savedInd != null && savedInd.SavedIndID > 0;

                EmailLog emailLog = DataUtils.GetOne<EmailLog>(ConstUtils.SP_GetOneEmailLog, new { SenderLoginID = Identity.LoginID, ReceiverLoginID = loginID });
                IsSentRecently = ViewModelUtils.IsSentRecently(emailLog);
            }
            else
            {
                IsIndSaved = false;
                IsSentRecently = false;
            }
            
        }

        
    }
}